var FolioId = "";
var ChargeIdString = "";
function CloseChragesdialog()
{
	$("#payment-charges").hide();
}

function CalCharge(){
	var value_charge=0;
	var ChargeInputPrice = $("[id*=payment-charges-inputprice]:visible").val();
	if(ChargeInputPrice==null)
		var ChargeInputPrice = $("[name=payment-chrages-charges_listchb]:visible option:selected").attr("price");
	var QtyValue = $("[id*=payment-charges-qty]:visible").val();
	var DiscountValue = $("[id*=payment-charges-discount]:visible").val()
	var TypeDiscount = $('input[name=group1]:visible:radio:checked')[0].defaultValue
	var TaxUse = $('[id*=payment-charges-tax]:visible:checked').val();
	var valueShow = 0;
	if(TypeDiscount=="cash"){
		if(TaxUse=="on"){
			valueShow = ((ChargeInputPrice*QtyValue)+(((ChargeInputPrice*QtyValue)*7)/100))-DiscountValue;
		}else{
			valueShow = (ChargeInputPrice*QtyValue)-DiscountValue;
		}
		$("[id*=payment-charges-balance]:visible").html(parseFloat(valueShow).toFixed(currencyDecimalPlaces));
	}else if(TypeDiscount=="percentage"){
		if(TaxUse=="on"){
			varlueShow = ((ChargeInputPrice*QtyValue)+(((ChargeInputPrice*QtyValue)*7)/100))-((ChargeInputPrice*QtyValue)*DiscountValue)/100;
		}else{
			varlueShow = (ChargeInputPrice*QtyValue)-((ChargeInputPrice*QtyValue)*DiscountValue)/100;
		}
		$("[id*=payment-charges-balance]:visible").html(parseFloat(varlueShow).toFixed(currencyDecimalPlaces));
	}
}

function getChargeInFolio(dataChargeInFolio){
	var objArrayChargeInFolio = new Array();
	objArrayChargeInFolio = JSON.parse(dataChargeInFolio);
	ChargeIdString = objArrayChargeInFolio.chargeString;
}
function editChargeInFolio(dataEditChargeInFolio){
	var objArrayEditChargeInFolio = new Array();
	objArrayEditChargeInFolio = JSON.parse(dataEditChargeInFolio);
}
/*function editFolioInCharge(dataEditFolioInCharge){
	var objArrayEditFolioInCharge= new Array();
	objArrayEditFolioInCharge = JSON.parse(dataEditFolioInCharge);
}*/
function EditCharge(){
	var ResIdset = $("[id*=payment-charges-group_owner]:visible").val();
	var typeSet=$("[id*=payment-charges-group_owner]:visible").find(":selected").attr("restype");
	var PosSet = $("[id*=payment-charges-pos]:visible").val();
	var ChargeInput = $("[id*=payment-charges-list]:visible").val();
	if(ChargeInput==null){
		var ChargeInput = $("[name=payment-chrages-charges_listchb]:visible option:selected").text();
	}
	var value_charge=0;
	var ChargeInputPrice = $("[id*=payment-charges-inputprice]:visible").val();
	if(ChargeInputPrice==null)
		var ChargeInputPrice = $("[name=payment-chrages-charges_listchb]:visible option:selected").attr("price");
	var QtyValue = $("[id*=payment-charges-qty]:visible").val();
	var DiscountValue = $("[id*=payment-charges-discount]:visible").val()
	var TypeDiscount = $('input[name=group1]:visible:radio:checked')[0].defaultValue
	var TaxUse = $('[id*=payment-charges-tax]:visible:checked').val();
	var valueShow = 0;
	var groupId=0;
	if(ResIdset==getCurrentTabData().resId){groupId=1;}
	if(TypeDiscount=="cash"){
		if(TaxUse=="on"){
			TaxUse = (((ChargeInputPrice*QtyValue)*7)/100);
			//value_charge = ((ChargeInputPrice*QtyValue)+(((ChargeInputPrice*QtyValue)*7)/100))-DiscountValue;
		}else{
			//value_charge = (ChargeInputPrice*QtyValue)-DiscountValue;
			TaxUse=0;
		}
	}else if(TypeDiscount=="percentage"){
		if(TaxUse=="on"){
			TaxUse = (((ChargeInputPrice*QtyValue)*7)/100);
		//	value_charge = ((ChargeInputPrice*QtyValue)+(((ChargeInputPrice*QtyValue)*7)/100))-((ChargeInputPrice*QtyValue)*DiscountValue)/100;
		}else{
		//	value_charge = (ChargeInputPrice*QtyValue)-((ChargeInputPrice*QtyValue)*DiscountValue)/100;
			DiscountValue = ((ChargeInputPrice*QtyValue)*DiscountValue)/100;
			TaxUse=0;
		}
	}
	ChargeInputPrice = ChargeInputPrice*QtyValue;
	var Tabs = getCurrentTabData();
	if(Tabs.type == "Single")
	{
		ResIdset = Tabs.resId;
		typeSet = Tabs.type;
		groupId =0;
	}
	var obj={objresId:ResIdset,objtypeSet:typeSet};
	var DateCharge = getServerDateTime().toSystemFormat()+" "+getServerDateTime().getHours()+":"+getServerDateTime().getMinutes();
	var url = "/chargeService?func=addCharge&description="+ChargeInput+"&chargeQty="+QtyValue+"&chargeAmount="+ChargeInputPrice
	+"&chargeDate="+DateCharge+"&discount="+DiscountValue+"&tax="+TaxUse+"&isRoomCharge=false&resId="+ResIdset+"&type="+typeSet+"&groupId="+groupId;
	simpleserviceWithArgs(url, null,addCharge_Success, obj)
}
 function addCharge_Success(data,obj)
 {
	var objArray = JSON.parse(data);
	var checkLogOut = CheckSession(objArray);
	if(checkLogOut)
		return;
	if(objArray  == null){
		simpleDialog(saveFail_label,100,null);
	}else{
		var Tabs = getCurrentTabData();
		if(obj.objtypeSet == "Single")
		{
			if(Tabs.resId != obj.objresId )
			{
				for(var i=0;i<Tabs.GroupResvList.length;i++)
				{
					if(Tabs.GroupResvList[i].resId == obj.objresId)
					{
						Tabs.GroupResvList[i].ChargeList.push(objArray);
						//$('[name*=Singlecheckbox_charge_resingroup]').parents("tr").remove();
						$('[name=chargeandpayment'+Tabs.GroupResvList[i].resId+']').after(fillChargeInGroupAccountStatement(null,Tabs.GroupResvList[i]));
					}
				}
			}else{
				var ch = {};
				for(var i = 0; i < objArray.length; ++i){
				ch[i] = objArray[i];
				Tabs.ChargeList.push(ch[i]);
				}
				fillChargeInAccountStatement();
			}	
		}else{
			(Tabs.ChargeList==null)?Tabs.ChargeList=objArray:Tabs.ChargeList.push(objArray);
			fillChargeInGroupAccountStatement(1,null);
		}
	}
 }
function isNumberKey(evt)
{
   var charCode = (evt.which) ? evt.which : event.keyCode
   if (charCode > 31 && (charCode < 48 || charCode > 57))
      return false;

   return true;
}